<template>
    <el-col :span="24">
        <el-col :span="12">
            <el-col class="mb20 box box-pack-start">
                <h3 style="font-size: 18px;">设置学校公章信息</h3>
            </el-col>
            <el-form ref="model" :model="model" :rules="ruless" label-width="100px">
                <el-form-item label="团队名称" prop="officialSeal">
                    <el-input v-model="model.officialSeal" placeholder="请填写团队名称"></el-input>
                </el-form-item>
                <el-form-item label="学校名称" prop="secondname">
                    <el-input v-model="model.secondname" placeholder="请填写学校名称"></el-input>
                </el-form-item>
                <el-form-item label="印章图片" prop="officialSealUrl" ref="image">
                    <el-upload class="avatar-uploader" :action="uploadfileurl" :show-file-list="false"
                        :on-progress="progress" :on-success="success" :before-upload="beforeUpload"
                        accept="image/jpeg,image/jpg,image/png,">
                        <el-image v-if="model.officialSealUrl" :src="model.officialSealUrl" class="avatar" fit="cover">
                        </el-image>
                        <div v-else class="upload-icon">
                            <i class="el-icon-upload"></i>
                            <div>上传</div>
                        </div>
                    </el-upload>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submit('model')">保 存</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-col>
</template>

<script>
    export default {
        data() {
            return {
                schoolid: this.$store.state.loginInfo.user.schoolId,
                model: {
                    id: this.$store.state.loginInfo.user.schoolId,
                    officialSeal: null,
                    officialSealUrl: null,
                    secondname:null
                },
                uploadfileurl: this.$url.baseURL + '/upload/uploadfile',
                ruless: {
                    officialSeal: [{
                        required: true,
                        message: '请填写团队名称',
                        trigger: 'blur'
                    }],
                    officialSealUrl: [{
                        required: true,
                        message: '请上传印章图片',
                        trigger: 'blur'
                    }],
                    secondname: [{
                        required: true,
                        message: '请填写学校名称',
                        trigger: 'blur'
                    }]
                }
            }
        },
        created() {
          this.getSchoolData()
        },
        methods: {
            getSchoolData(){
                let url = '/yschool/getById';
                let params = {
                    id:this.$store.state.loginInfo.user.schoolId
                };
                this.$ajax.post(url, params).then(res => {
                    if (res.code == 0) {
                        console.log(res);
                        this.model={
                            id:res.info.id,
                            officialSealUrl:res.info.officialSealUrl,
                            officialSeal:res.info.officialSeal,
                            secondname:res.info.secondname
                        }
                    } else {
                        this.$message.error(res.code)
                    }
                })
            },
            progress(event) {
                this.showProgress = true;
                this.progInt = parseInt(event.loaded / event.total * 100, 10);
            },
            success(response, file, fileList) {
				if(response.state.code == 0){
					this.model.officialSealUrl = response.body.data.url;
				}
                
                this.$refs['image'].clearValidate()
            },
            beforeUpload(file) {
                const fileName = file.name;
                const index = fileName.lastIndexOf(".");
                const ext = fileName.substr(index + 1);
                const arr = ["jpg", "png", "gif", "jpeg"];
                const isOk = arr.includes(ext);
                if (!isOk) {
                    this.$message.warning('封面格式暂不支持！');
                    return false;
                }
                var imgSize = file.size / (1024 * 1024);
                if (imgSize > 50) {
                    this.$message.warning(`当前文件${ parseInt(imgSize) }M,上传文件不能超过50M`)
                    return false
                }
            },
            submit(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        const loading = this.$loading({
                            lock: true,
                            text: 'Loading',
                            spinner: 'el-icon-loading',
                            background: 'rgba(0, 0, 0, 0.3)'
                        });
                        let url = '/yschool/modify';
                        let params = this.model;
                        console.log(params);
                        this.$ajax.post(url, params).then(res => {
                            loading.close();
                            if (res.code == 0) {
                                this.$message.success('保存成功')
                            } else {
                                this.$message.error(res.code)
                            }
                        })

                    } else {
                        return false;
                    }
                });
            },
        }
    }
</script>

<style scoped>
    .avatar {
            width: 110px;
            height: 110px;
        }
</style>
